/*
 * Development of Selenium extension used to mimic file uploads.
 */

Selenium.prototype.doTypeSampleFileName = function(locator, text) {
    var element = this.page().findElement(locator);

    // Create a hidden input of simillar name to ``element``.
    // XXX Where is this tested from?
    // XXX Should check if an element of the name what we want to
    //     create already exists?
    // XXX Should check that ``element`` is of type "file"?
    var form = element.form;
    var name = element.name + "__ZELENIUM";
    // XXX Unsure if ``document`` is the correct object to be creating
    //     the new element from. Since we are in a frame based world.
    var input = document.createElement("INPUT");
    input.name = name;
    input.type = "hidden";
    form.appendChild(input);

    // Now use this new input:
    this.page().replaceText(input, text);
};

/*
 * Load content from a file and replace the locator node with this.
 * The content file should have an id="newcontent" on the element root
 * to use as the replacement. The content file needs to be xhtml and load
 * the xhtml DTD, otherwise getElementById will not work since an attribute
 * of type ID won't have been defined. Thankfully firefox reads the DTD
 * for xhtml.
 */

Selenium.prototype.doGetContentAndReplaceNode = function(contentfile, replaceLocator) {
    req = new XMLHttpRequest();
    req.overrideMimeType('text/xml');
    req.open("GET",contentfile,false);
    req.send(null);
    var responsexml = req.responseXML;
    var element = this.page().findElement(replaceLocator);
    var parent = element.parentNode;
    var replacementnode = responsexml.getElementById('newcontent')
    element.parentNode.replaceChild(replacementnode,element);
};

/*
 * Load a Javascript file now. Useful for monkey patching
 *
 */

Selenium.prototype.doLoadJavascriptFile = function(url) {

   targetdocument = selenium.browserbot.currentWindow.document
   var scriptelement = targetdocument.createElement("script");
   scriptelement.src = url;
   scriptelement.type="text/javascript";
   targetdocument.getElementsByTagName("head")[0].appendChild(scriptelement);
}
